<?php
/**
 * Create By guaosi
 * Author guaosi
 * Date: 2017/11/3/0003
 * Time: 13:44
 */
namespace app\admin\controller;
use app\admin\model\Comment as CommentModel;
use think\Request;

class Comment extends Base
{
    /**
     * 评论列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $data=$request->except('/admin/comment/index_html');
//       $list=\app\admin\model\News::getNewsPage($pagesize);
//       将传递过来的数组参数变成url后面的参数形式
//        用于laypage
        $url=http_build_query($data);
        $this->setPageAndSize($data);

        $whereData=[];
        if(!isset($data['status'])||$data['status']==2)
        {
            $whereData['status']=[
                'neq',config('admin.status_delete')
            ];
        }
        else
        {
            $whereData['status']=[
                'eq',$data['status']
            ];
        }
        if(!empty($data['news_id']))
        {
            $whereData['news_id']=[
                'eq',$data['news_id']
            ];
        }
        if(!empty($data['start_time'])&&!empty($data['end_time']))
        {
            $start_time=strtotime($data['start_time']);
            $end_time=strtotime($data['end_time']);
            if($start_time<$end_time)
            {
                $whereData['create_time']=[
                    ['gt',$start_time],
                    ['lt',$end_time]
                ];
            }
        }
        elseif (!empty($data['start_time']))
        {
            $whereData['create_time']=[
                'gt',strtotime($data['start_time'])
            ];
        }
        elseif(!empty($data['end_time']))
        {
            $whereData['create_time']=[
                'lt',strtotime($data['end_time'])
            ];
        }

        $list=CommentModel::getListByTp5Model($whereData,$this->from,$this->pageSize);
        $totalCount=CommentModel::getCountsByCondition($whereData);
        $pageTotal=ceil($totalCount/$this->pageSize);
        return $this->fetch('',[
            'list'=>$list,
            'total'=>$pageTotal,
            'curr'=>$this->page,
            'url'=>$url,
            'status'=>!isset($data['status'])?2:$data['status'],
            'start_time'=>empty($data['start_time'])?'':$data['start_time'],
            'end_time'=>empty($data['end_time'])?'':$data['end_time'],
            'news_id'=>empty($data['news_id'])?'':$data['news_id']
        ]);
    }
    public function changeStatus()
    {
       parent::changeStatus(); // TODO: Change the autogenerated stub
        $request=Request::instance();
        $data=$request->param();
        $comment=CommentModel::where('id','=',$data['id'])->find();
        $news=CommentModel::with(['news'])->where('id',$data['id'])->find();
        if($comment->status===1)
        {
            $news->news->comment_count=$news->news->comment_count+1;
        }
         else
         {
             $news->news->comment_count=$news->news->comment_count-1;
         }
        try{
            if(($news->news->save())!==false)
            {
                $result=[
                    'code'=>400,
                    'msg'=>'状态修改成功',
                    'errorCode'=>1,
                    'jump_url'=>$_SERVER['HTTP_REFERER']
                ];
                return json($result);
            }
            throw new Exception('状态修改失败...');
        }catch (\Exception $e)
        {
            $result=[
                'code'=>404,
                'msg'=>$e->getMessage(),
                'errorCode'=>0
            ];
            return json($result);
        }
    }
    public function toDelete()
    {
        $obj=parent::toDelete(); // TODO: Change the autogenerated stub
        $request=Request::instance();
        $data=$request->param();
        $news=CommentModel::with(['news'])->where('id',$data['id'])->find();
        $comment=CommentModel::where('id','=',$data['id'])->find();
//        $ori_status=$obj->ori_status;
        $resultObj=$obj->getData();
        if($resultObj['ori_status']===1)
        {
            $news->news->comment_count=$news->news->comment_count-1;
        }
        try{
            if(($news->news->save())!==false)
            {
                $result=[
                    'code'=>400,
                    'msg'=>'状态修改成功',
                    'errorCode'=>1,
                    'jump_url'=>$_SERVER['HTTP_REFERER']
                ];
                return json($result);
            }
            throw new Exception('状态修改失败...');
        }catch (\Exception $e) {
            $result = [
                'code' => 404,
                'msg' => $e->getMessage(),
                'errorCode' => 0
            ];
            return json($result);

        }
    }
}